package ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sxpjyy.monkey.mobilenurse.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapters.JCJGListAdapter;
import mytools.MyBaseActivity;
import objects.InPatient;
import objects.JCJGDetail;
import service.WebService;

public class ReportJCActivity extends MyBaseActivity implements SwipeRefreshLayout.OnRefreshListener, ExpandableListView.OnGroupClickListener {

    private Map<String, List<JCJGDetail>> mJCDataSet;

    private SwipeRefreshLayout swipeRefreshLayout;
    private ExpandableListView expandableListView;
    private JCJGListAdapter mJCAdapter;

    private String ZYHM;

    private boolean[] isTaskExecuting = {false, false, false, false, false};
    private boolean[] hasResult = {false, false, false, false, false};
    private String[] parentsArray;
    private String[] mJCJGResults = {"", "", "", "", ""};

    private List<JCJGDetail> mRISList;
    private List<JCJGDetail> mUISList;
    private List<JCJGDetail> mECGList;
    private List<JCJGDetail> mESList;
    private List<JCJGDetail> mPISList;

    private static ProgressDialog progressDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_jc);
        onBindUI();

        mJCAdapter = new JCJGListAdapter(this);
        expandableListView.setAdapter(mJCAdapter);
        expandableListView.setOnGroupClickListener(this);

        swipeRefreshLayout.setColorSchemeResources(R.color.holo_green_dark, R.color.holo_green_light, R.color.holo_orange_light, R.color.holo_red_light);
        swipeRefreshLayout.setOnRefreshListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        InPatient inPatient = getMyApplication().getInPatient();
        parentsArray = getResources().getStringArray(R.array.pacs_type);
        if (inPatient != null) {
            ZYHM = inPatient.getZYHM();
            doExecute();
        }
    }

    private void onBindUI() {
        swipeRefreshLayout = findViewById(R.id.jc_swipe_ly);
        expandableListView = findViewById(R.id.jc_expandable_lv);
    }

    @Override
    public void onRefresh() {
        if (ZYHM != null) {
            doExecute();
        }
    }

    //显示等待对话框
    private static void showProgressBar(Context context) {
        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("正在获取信息。。。");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    //取消等待对话框
    private static void cancelProgressBar() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.cancel();
            progressDialog = null;
        }
    }

    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View view, int position, long l) {
        if (expandableListView.getId() == R.id.jc_expandable_lv) {
            if (mJCJGResults[position] == null || mJCJGResults[position].equals(""))
                Toast.makeText(ReportJCActivity.this, parentsArray[position] + " 报告项目为空！", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    /**
     * RIS检查报告(放射)
     */
    private class MyRISTask extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            isTaskExecuting[0] = true;
        }

        @Override
        protected String doInBackground(Void... voids) {
            WebService RISWebService = new WebService(ReportJCActivity.this);
            RISWebService.setMETHOD_NAME("getRISResult");
            String result;
            String[] Parms = {ZYHM};
            RISWebService.setPARMS(Parms);
            result = RISWebService.getServerData();
            if (result == null || result.equals("null") || result.equals("") || result.equals("[]")) {
                return "获取RIS数据失败！";
            } else {
                if (!result.equals(mJCJGResults[0])) {
                    mJCJGResults[0] = result;
                    Gson gson = new Gson();
                    mRISList = gson.fromJson(mJCJGResults[0], new TypeToken<List<JCJGDetail>>() {
                    }.getType());
                    if (mRISList != null && mRISList.size() > 0) {
                        hasResult[0] = true;
                        return null;
                    } else {
                        return "获取RIS数据为空！";
                    }
                } else {
                    return "RIS数据未发生变化";
                }
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                hasResult[0] = false;
                Toast.makeText(ReportJCActivity.this, s, Toast.LENGTH_SHORT).show();
            }
            isTaskExecuting[0] = false;
            mJCDataSet.put(parentsArray[0], mRISList);
            doLoadData();
        }
    }

    /**
     * UIS检查报告(超声)
     */
    private class MyUISTask extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            isTaskExecuting[1] = true;
        }

        @Override
        protected String doInBackground(Void... voids) {
            WebService UISWebService = new WebService(ReportJCActivity.this);
            UISWebService.setMETHOD_NAME("getUISResult");
            String result;
            String[] Parms = {ZYHM};
            UISWebService.setPARMS(Parms);
            result = UISWebService.getServerData();
            if (result == null || result.equals("null") || result.equals("") || result.equals("[]")) {
                return "获取UIS数据失败！";
            } else {
                if (!result.equals(mJCJGResults[1])) {
                    mJCJGResults[1] = result;
                    Gson gson = new Gson();
                    mUISList = gson.fromJson(mJCJGResults[1], new TypeToken<List<JCJGDetail>>() {
                    }.getType());
                    if (mUISList != null && mUISList.size() > 0) {
                        hasResult[1] = true;
                        return null;
                    } else {
                        return "获取UIS数据为空！";
                    }
                } else {
                    return "数据未发生变化";
                }
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                hasResult[1] = false;
                Toast.makeText(ReportJCActivity.this, s, Toast.LENGTH_SHORT).show();
            }
            isTaskExecuting[1] = false;
            mJCDataSet.put(parentsArray[1], mUISList);
            doLoadData();
        }
    }

    /**
     * ECG检查报告(心电)
     */
    private class MyECGTask extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            isTaskExecuting[2] = true;
        }

        @Override
        protected String doInBackground(Void... voids) {
            WebService ECGWebService = new WebService(ReportJCActivity.this);
            ECGWebService.setMETHOD_NAME("getECGResult");
            String result;
            String[] Parms = {ZYHM};
            ECGWebService.setPARMS(Parms);
            result = ECGWebService.getServerData();
            if (result == null || result.equals("null") || result.equals("") || result.equals("[]")) {
                return "获取ECG数据失败！";
            } else {
                if (!result.equals(mJCJGResults[2])) {
                    mJCJGResults[2] = result;
                    Gson gson = new Gson();
                    mECGList = gson.fromJson(mJCJGResults[2], new TypeToken<List<JCJGDetail>>() {
                    }.getType());
                    if (mECGList != null && mECGList.size() > 0) {
                        hasResult[2] = true;
                        return null;
                    } else {
                        return "获取ECG数据为空！";
                    }
                } else {
                    return "数据未发生变化";
                }
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                hasResult[2] = false;
                Toast.makeText(ReportJCActivity.this, s, Toast.LENGTH_SHORT).show();
            }
            isTaskExecuting[2] = false;
            mJCDataSet.put(parentsArray[2], mECGList);
            doLoadData();
        }
    }

    /**
     * ES检查报告(内镜)
     */
    private class MyESTask extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            isTaskExecuting[3] = true;
        }

        @Override
        protected String doInBackground(Void... voids) {
            WebService ESWebService = new WebService(ReportJCActivity.this);
            ESWebService.setMETHOD_NAME("getESResult");
            String result;
            String[] Parms = {ZYHM};
            ESWebService.setPARMS(Parms);
            result = ESWebService.getServerData();
            if (result == null || result.equals("null") || result.equals("") || result.equals("[]")) {
                return "获取ES数据失败！";
            } else {
                if (!result.equals(mJCJGResults[3])) {
                    mJCJGResults[3] = result;
                    Gson gson = new Gson();
                    mESList = gson.fromJson(mJCJGResults[3], new TypeToken<List<JCJGDetail>>() {
                    }.getType());
                    if (mESList != null && mESList.size() > 0) {
                        hasResult[3] = true;
                        return null;
                    } else {
                        return "获取ES数据为空！";
                    }
                } else {
                    return "数据未发生变化";
                }
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                hasResult[3] = false;
                Toast.makeText(ReportJCActivity.this, s, Toast.LENGTH_SHORT).show();
            }
            isTaskExecuting[3] = false;
            mJCDataSet.put(parentsArray[3], mESList);
            doLoadData();
        }
    }

    /**
     * PIS检查报告(病理)
     */
    private class MyPISTask extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            isTaskExecuting[4] = true;
        }

        @Override
        protected String doInBackground(Void... voids) {
            WebService PISWebService = new WebService(ReportJCActivity.this);
            PISWebService.setMETHOD_NAME("getPISResult");
            String result;
            String[] Parms = {ZYHM};
            PISWebService.setPARMS(Parms);
            result = PISWebService.getServerData();
            if (result == null || result.equals("null") || result.equals("") || result.equals("[]")) {
                return "获取PIS数据失败！";
            } else {
                if (!result.equals(mJCJGResults[4])) {
                    mJCJGResults[4] = result;
                    Gson gson = new Gson();
                    mPISList = gson.fromJson(mJCJGResults[4], new TypeToken<List<JCJGDetail>>() {
                    }.getType());
                    if (mPISList != null && mPISList.size() > 0) {
                        hasResult[4] = true;
                        return null;
                    } else {
                        return "获取PIS数据为空！";
                    }
                } else {
                    return "PIS数据未发生变化";
                }
            }
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                hasResult[4] = false;
                Toast.makeText(ReportJCActivity.this, s, Toast.LENGTH_SHORT).show();
            }
            isTaskExecuting[4] = false;
            mJCDataSet.put(parentsArray[4], mPISList);
            doLoadData();
        }
    }

    /**
     * 加载数据
     */
    private void doLoadData() {
        for (int i = 0; i < 5; i++) {
            if (isTaskExecuting[i]) {
                return;
            }
        }
        cancelProgressBar();
        if (swipeRefreshLayout.isNestedScrollingEnabled()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        mJCAdapter.setDataSet(mJCDataSet);
        mJCAdapter.notifyDataSetChanged();
        for (int i = 0; i < 5; i++) {
            if (hasResult[i]) {
                expandableListView.expandGroup(i);
            }
        }
    }

    private void doExecute() {
        mJCDataSet = new HashMap<>();
        showProgressBar(ReportJCActivity.this);
        new MyRISTask().execute();
        new MyUISTask().execute();
        new MyECGTask().execute();
        new MyESTask().execute();
        new MyPISTask().execute();
    }
}
